<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");

$log = new Logger("./../orderdetailsapi.log");
$common = new Common();
$ordercom = new OrderCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$appointcom = new AppointCom();
$serviceinfocom = new ServiceinfoCom();

if (!isset($_REQUEST['orderid']) || empty($_REQUEST['orderid'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find orderid"));
    exit();
}

$orderid = $_REQUEST['orderid'];
//测试数据
//$orderid = 1;

$orderdetail = $ordercom->getOrderById($orderid);
if (!$orderdetail) {
    echo json_encode(array("code" => 400, "msg" => "get orderdetail failed"));
    exit();
}

$result = array("code" => 0, "msg" => "success", "data" => array());

$result["data"]['tradeno'] = $orderdetail['trade_no'];
$result["data"]['codeid'] = $orderdetail['codeid'];

if ($orderdetail['sex'] == 1) {
    $result["data"]["sex"] = "男";
} else {
    $result["data"]["sex"] = "女";
}

$result["data"]['mobile'] = $orderdetail['mobile'];
////获取最新的预约时间
//$appoint = $appointcom->getAppointDesc($orderid);
//if (!$appoint) {
//    echo json_encode(array("code" => 400, "msg" => "get appoint failed"));
//    exit();
//}
//$aa = $reservetimecom->getRtimeById($appoint['reservetime_id']);
//$time = date("m-d", $appoint['time']);
//$result["data"]["reservetime"] = $time . " " . $aa['info'];

$result["data"]["totalfee"] = sprintf("%.2f", $orderdetail['totalfee'] / 100);
$result["data"]["name"] = $orderdetail['username'];
if ($orderdetail['is_pay'] == 0) {
    $result["data"]["status"] = "未支付";
} else {
    $result["data"]["status"] = "已支付";
}
$result["data"]["ispay"] = $orderdetail['is_pay'];
$servicearr = $serviceinfocom->getServiceinfoByOrderid($orderid);
if (!$servicearr) {
    echo json_encode(array("code" => 400, "msg" => "get serviceinfo failed"));
    exit();
}
$result["data"]["serviceinfo"] = array();
foreach ($servicearr as $k => $v) {
    $servicename = $servicecom->getServiceById($v['service_id']);
    $result["data"]["serviceinfo"][$k]["service"] = $servicename['name'];
    $result["data"]["serviceinfo"][$k]["number"] = $v['total'];
    //$result["data"]["serviceinfo"][$k]["usednum"] = $v['used_num'];
}

$resjson = json_encode($result);
$log->debug("orderdetail : " . $resjson);
echo $resjson;
exit();
